package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;


import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_TOMB_REPAIR_INFO, desc = "秦俑再世请求地区修复进度")
public class TombRepairProcessCmd extends AbstractCommand {
    private Tomb.TombRepairInfoSync.Builder syncRespBuilder;

    private Tomb.TombRepairInfoReq req;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        req = Tomb.TombRepairInfoReq.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.syncRespBuilder = Tomb.TombRepairInfoSync.newBuilder();
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.syncRespBuilder.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_REPAIR_INFO, this.syncRespBuilder);
            return;
        }


        Tomb.TombRepairInfoReq.Builder reqBuilder = req.toBuilder();
        reqBuilder.setUnionUid(player.getUserInfo().getUnionUid());
        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(), CrossProtocol.C_TOMB_REPAIR_PROCESS_SYNC, activityInfo.getActivityId(), reqBuilder, this.syncRespBuilder);
        future.thenAccept(syncResponse -> {
            if (!this.syncRespBuilder.hasRet()) {
                this.syncRespBuilder.setRet(0);
            }
            if (this.syncRespBuilder.getRet() != 0) {
                player.sendPacket(ClientProtocol.U_TOMB_REPAIR_INFO, this.syncRespBuilder);
            }
            Map<Integer, Long> progressMap = new ConcurrentHashMap<>();
            //用商会当前的进度值*100去除最大值
            for (Tomb.TombRepairProgress progress : syncRespBuilder.getRepairDataList()) {
                int maxProgressByRepairZoneId = TombMgr.getMaxProgressByRepairZoneId(progress.getId());
                if (maxProgressByRepairZoneId != 0){
                    progressMap.put(progress.getId(),progress.getProgress()*100 / maxProgressByRepairZoneId);
                }else {
                    progressMap.put(progress.getId(),0L);
                }
            }
            //先提醒更新商会成就
            player.notifyListener(eGamePlayerEventType.TombUnionNianTuNum.getValue(), progressMap);
            player.sendPacket(ClientProtocol.U_TOMB_REPAIR_INFO, this.syncRespBuilder);
        });
    }

    @Override
    public void sendData(GamePlayer player) {

    }

    @Override
    public boolean isDebug() {
        return false;
    }

    @Override
    public String getRequestData() {
        return super.getRequestData();
    }

}
